#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_WriteDataById_0x2E_test.py
@Time    :   2022/9/13 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from gettext import find
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SID_0x19_reportDTCByStatusMask_noFault = [([0x19, 0x02, 0x01], [0x59, 0x02, 0x09], [0x19, 0x02, 0x01]),
                                          ([0x19, 0x02, 0x08], [0x59, 0x02, 0x09], [0x19, 0x02, 0x08]),
                                          ([0x19, 0x02, 0x09], [0x59, 0x02, 0x09], [0x19, 0x02, 0x09]),
                                          ([0x19, 0x02, 0x0A], [0x59, 0x02, 0x09], [0x19, 0x02, 0x0A]),
                                          ([0x19, 0x02, 0x0B], [0x59, 0x02, 0x09], [0x19, 0x02, 0x0B]),
                                          ([0x19, 0x02, 0xFF], [0x59, 0x02, 0x09], [0x19, 0x02, 0xFF])]

class Test_reportDTCByStatusMask(object):

   '''NO Fault'''
   ids = ["Request {2}".format(req, resp, binascii.b2a_hex(bytearray(send), ' ')) for req, resp, send in SID_0x19_reportDTCByStatusMask_noFault]

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.parametrize('req,resp,title', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_Default_Phy(self, action, BCMPower_Gear, Driver, req, resp, title):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.parametrize('req,resp,title', SID_0x19_reportDTCByStatusMask_noFault, ids=ids)
   def test_reportDTCByStatusMask_No_Fault_extended_Phy(self, action, BCMPower_Gear, Driver, req, resp, title):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)


   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.repeat(10)
   def test_reportDTCByStatusMask_No_Fault_Restart_withPowerOff_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", "59 02 09", 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
      iStep = Driver['CAN'].restartMRR_Power(Driver,iStep)
      time.sleep(2)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", "59 02 09", 0, iStep, False, 'UDS', 'noClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.repeat(10)
   def test_reportDTCByStatusMask_No_Fault_Restart_with11_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", "59 02 09", 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)
      iStep = Driver['CAN'].restartMRR_11(Driver,iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "19 02 09", "59 02 09", 0, iStep, False, 'UDS', 'noClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

   
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   def test_DTC_VBAT_HIGH_ByStatusMask_defaul_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      time.sleep(0.5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 1, iStep, False, 'UDS', 'ClearFault')

      # 设置电压为17V，并等待1.5s 制造 Overvoltage 故障，读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09,0xAC,0xCE,0x17,0x09], 0, iStep, False, 'UDS', 'noClearFault')

      # 设置电压为12V，并等待1.5s 恢复 Overvoltage 故障，读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 0, iStep, False, 'UDS', 'noClearFault')

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   def test_DTC_VBAT_LOW_ByStatusMask_extended_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      time.sleep(0.5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 3, iStep, False, 'UDS', 'ClearFault')

      # 设置电压为8V，并等待1.5s 制造 Undervoltage 故障，读取当前故障 
      iStep = Driver['CAN'].SetVolt(Driver, 8, 1.5, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09,0xAC,0xCF,0x16], 0, iStep, False, 'UDS', 'noClearFault')

      # 设置电压为12V，并等待1.5s 恢复 Undervoltage 故障，读取当前故障
      iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 0, iStep, False, 'UDS', 'noClearFault')
   
   
   
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   def test_DTC_SFCAN_BUSOFF_ByStatusMask_defaul_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      time.sleep(1.5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 1, iStep, False, 'UDS', 'ClearFault')
      iStep = Driver['CAN'].delInitialFailure(iStep,uds_resp)

      # 短接sCAN_H与sCAN_L 制造 DTC_SFCAN_BUSOFF 故障，读取当前故障 
      iStep = Driver['CAN'].S_ConnectCanH_CanL(True, iStep)
      # 断开sCAN_H与sCAN_L短接 恢复 DTC_SFCAN_BUSOFF 故障，读取当前故障
      iStep = Driver['CAN'].S_ConnectCanH_CanL(False, iStep)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x09], [0x59,0x02,0x09,0xC1,0xD7,0x88,0x08], 0, iStep, False, 'UDS', 'noClearFault')
      time.sleep(5)
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x19,0x02,0x01], [0x59,0x02,0x09], 0, iStep, False, 'UDS', 'noClearFault')
